<?php

namespace Common;

use Common\Models\WbInviteCodes;
use Common\Models\WbUser;
use Common\Models\WbKeyValue;
use Common\Models\WbUserMiner;
use Common\Models\WbUsersExtend;
use Common\Models\WbUserBeanSilverLog;
use Common\Models\WbUsersWallets;

/**
 * Class CommonController 工具类
 */
class ToolsController
{

    const EXPIRE_HOUR=2000;        //token过期时间
    const KEY = 'UF((&)(&^#&)@!';//ASE解密密码

    //生成mid加密
    public static function MakeToken($uid){
        $token = md5(microtime(true).$uid);
        return $token;
    }

    //Token加密
    public static function TokenEncrypt($uid, $token){
        #UF997097637021
        $key = 'UF((&)(&^#&)@!';
        $encryptToken = base64_encode(openssl_encrypt("{$uid}:{$token}", 'DES-EDE3', $key, OPENSSL_RAW_DATA));
        return $encryptToken;
    }



    /**
     * @func 获取一个时间节点
     * @param $time_type 1 今日 2 昨日 3 七天 4 15天 5 30天 6 60天 7 90天 8 本月 9 上月 10前天 12今年 13前年
     * @return array
     */
    public static function timeChange($time_type)
    {
        switch ($time_type) {
            case 1:
                //获取今日开始时间戳和结束时间戳
                $start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
                break;
            case 2:
                //获取昨日开始时间戳和结束时间戳
                $start = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
                $end = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
                break;
            case 3:
                //获取七天开始时间戳和结束时间戳
                $start = mktime(0, 0, 0, date('m'), date('d') - 6, date('Y'));
                $end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                break;
            case 4:
                //获取15天
                $start = mktime(0, 0, 0, date('m'), date('d') - 14, date('Y'));
                $end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                break;
            case 5:
                //获取30天
                $start = mktime(0, 0, 0, date('m'), date('d') - 29, date('Y'));
                $end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                break;
            case 6:
                //获取60天
                $start = mktime(0, 0, 0, date('m'), date('d') - 59, date('Y'));
                $end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                break;
            case 7:
                //获取90天
                $start = mktime(0, 0, 0, date('m'), date('d') - 89, date('Y'));
                $end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                break;
            case 8:
                //获取本月
                $start = mktime(0, 0, 0, date('m'), 1, date('Y'));
                $end = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
                break;
            case 9:
                //获取上月
                $start = mktime(0, 0, 0, date('m')-1, 1, date('Y'));
                $end = mktime(23, 59, 59, date('m'), 1-1, date('Y'));
                break;
            case 10:
                //获取前天开始时间戳和结束时间戳
                $start = mktime(0, 0, 0, date('m'), date('d') - 2, date('Y'));
                $end = $start+86399;
                break;
            case 11:
                $time=time();
                $time = $time != '' ? $time : time();
                //组合数据
                $date = [];
                for ($i=1; $i<=7; $i++){
                    $mydata = date('Y-m-d' ,strtotime( '+' . $i-7 .' days', $time));
                    $date[$i]['datatime']=$mydata;
                    $date[$i]['start_at'] = $mydata.' 00:00:00';
                    $date[$i]['end_at'] = $mydata.' 23:59:59';
                }
                return $date;
                break;
            case 12:
                $start = mktime(0, 0,0, 1, 1, date('Y', time()));
                $end =mktime(0, 0, 0, 12, 31, date('Y', time()));
                break;
            case 13:
                $date = date('Y-m-d H:i:s');
                $start = strtotime(date(date("Y-01-01 00:00:00",strtotime("$date -1 year"))));
                $end = strtotime(date('Y-12-31 23:59:59', strtotime("$date -1 year")));
                break;
        }
        $times = array('start_at' => date("Y-m-d H:i:s", $start), 'end_at' => date("Y-m-d H:i:s", $end));
        return $times;
    }


    /**
     * 分页
     */
    public static function paging($list,$count=0,$page_size=0,$page=0){
        if($count==0 && $page_size==0 && $page==0){
            $list = json_encode($list);
            return json_decode($list,true);
        }
        if(round($count/$page_size) == 0){
            $total_page = 1;
        }else{
            $total_page = $count/$page_size;
        }
        $data['items'] = $list;
        $data['first'] = 1;
        $data['before'] = $page>1 ? $page-1:$page;
        $data['current'] = $page;
        $data['last'] =  $count>0 ? (int)$total_page : 0;
        $data['next'] = (int)$total_page > $page+1 ? $page+1 : $page;
        $data['total_pages'] = $count>0 ? (int)$total_page : 0;
        $data['total_items'] = $count;
        $data['limit'] = $page_size;
        $data = json_decode(json_encode($data),true);
        return $data;
    }



    /**
     * @启用账号，直邀，联盟的人数加一
     * @param $path 路径
     * @param $referer_id 父级id
     */
    public static function invite_add_one($user_id,$path){     //父级id
        $user = new Member();
        $referer_user = $user->find_by_id($user_id);
        if($referer_user) {
            $referer_user['direct_vip'] = $referer_user['direct_vip'] + 1;      //父级直邀人数加1
            $referer_user['team_scale'] = $referer_user['indirect_vip'] + $referer_user['direct_vip'];
            $user0 = new Member();
            $user0->save_member($referer_user);
            if ($path) {
                $indirect_ids = explode(',', $path);
                foreach ($indirect_ids as $key => $value) {
                    $user1 = new Member();
                    $indirect_user = $user->find_by_id($value);
                    if ($indirect_user) {
                        $indirect_user['indirect_vip'] = $indirect_user['indirect_vip'] + 1;    //path里面的人数加1
                        $indirect_user['team_scale'] = $indirect_user['direct_vip'] + $indirect_user['indirect_vip'];
                        $user1->save_member($indirect_user);
                    }
                }
            }
        }
    }


    /**
     * @启用账号，直邀，联盟的人数加一
     * @param $path 路径
     * @param $referer_id 父级id
     */
    public static function invite_subtraction_one($user_id,$path){     //父级id
        $user = new Member();
        $referer_user = $user->find_by_id($user_id);
        if($referer_user) {
            if($referer_user['direct_vip']>0) {
                $referer_user['direct_vip'] = $referer_user['direct_vip'] - 1;      //父级直邀人数加1
            }else{
                $referer_user['direct_vip'] = 0;
            }
            $referer_user['team_scale'] = $referer_user['indirect_vip'] + $referer_user['direct_vip'];
            $user0 = new Member();
            $user0->save_member($referer_user);
            if ($path) {
                $indirect_ids = explode(',', $path);
                foreach ($indirect_ids as $key => $value) {
                    $user1 = new Member();
                    $indirect_user = $user->find_by_id($value);
                    if ($indirect_user) {
                        $indirect_user['indirect_vip'] = $indirect_user['indirect_vip'] - 1;    //path里面的人数加1
                        $indirect_user['team_scale'] = $indirect_user['direct_vip'] + $indirect_user['indirect_vip'];
                        $user1->save_member($indirect_user);
                    }
                }
            }
        }
    }

}
